#include "collectionitemmodel.h"

CollectionItemModel::CollectionItemModel(Collection * collection)
{
    this->collection = collection;

    state = 1;

    artists = collection->getArtists();

}

QModelIndex CollectionItemModel::index(int row, int column, const QModelIndex & parent) const
{
        return createIndex(row,column);
}

QModelIndex CollectionItemModel::parent(const QModelIndex & index ) const
{
    return QModelIndex();
}

int CollectionItemModel::rowCount(const QModelIndex & parent) const
{
    if(state == 1){
        return artists->size();
    }else if(state == 2){
        return albums->size();
    }else if(state == 3){
        return songs->size();
    }

    return 0;
}

int CollectionItemModel::columnCount(const QModelIndex & parent) const
{
    return 1;
}

QVariant CollectionItemModel::data(const QModelIndex & index, int role) const
{

    if(role == Qt::DisplayRole){
        if(state == 1){
            return artists->at(index.row())->getName();
        }else if(state == 2){
            return albums->at(index.row())->getName();
        }else if(state == 3){
            return songs->at(index.row())->title;
        }
    }/*else if(role == Qt::UserRole){
        if(state == 1){
            return qVariantFromValue( (void*) artists->at(index.row()) );
        }else if(state == 2){
            return qVariantFromValue( (void*) albums->at(index.row()) );
        }else if(state == 3){
            return qVariantFromValue( (void*) songs->at(index.row()) );
        }
    }*/
    
    return QVariant();
}

void CollectionItemModel::clickedCallback(QModelIndex index)
{
    qDebug() << "clicked!";

    if(state == 1){
        albums = collection->getAlbums(artists->at(index.row()));
        state = 2;
    }else if(state == 2){
        songs = collection->getSongs(albums->at(index.row()));
        state = 3;
    }else if(state == 3){
        qDebug() << "play!";
        emit this->songSelected(songs->at(index.row()));

        /*
        if(songs->at(index.row())->getAlbum() != NULL){
            qDebug() << songs->at(index.row())->getAlbum()->getName();
        }else if(songs->at(index.row())->getArtist() != NULL){
            qDebug() << songs->at(index.row())->getArtist()->getName();
        }else{
            qDebug() << songs->at(index.row())->file;
        }
        */
    }

    this->reset();
}

void CollectionItemModel::previous()
{
    if(state > 1){
        state--;
    }else{
        emit atRoot();
    }
    this->reset();
}
